<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                新增用户
            </el-header>
            <el-main>
                <el-form label-width="110px" ref="user" :rules="rules" :model="user" size="mini"
                         :label-position="formAlignLeft">
                    <el-form-item label="姓名 : " prop="name">
                        <el-input v-model="user.name" placeholder="请输入姓名 : "></el-input>
                    </el-form-item>
                    <el-form-item label="性别 : " prop="sex">
                        <el-select v-model="user.sex" placeholder="请选择性别 : " style="width: 100%;">
                            <el-option
                                    v-for="(val, key) in sexOptions"
                                    :key="key"
                                    :label="val"
                                    :value="key">
                                {{val}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="手机号 : " prop="codePhoneNumber">
                        <el-input v-model="user.codePhoneNumber" placeholder="请输入手机号 : "></el-input>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        完成新增
                    </el-button>
                    <el-button @click="editOut">
                        取消新增
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../../utils/webUtils.js';

    export default {
        name: "addUser",
        data() {
            return {
                formAlignLeft: 'left',
                uuid: '',
                user: {
                    name: '',
                    sex: '',
                    birthday: '',
                    vouPhoneNumber: 'Mobile',
                    codePhoneNumber: ''
                },
                sexOptions: {},
                rules: {
                    name: [
                        {required: true, message: '姓名不得为空', trigger: 'blur'},
                    ],
                    sex: [
                        {required: true, message: '性别不得为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        mounted() {
            this.querySex();
        },
        methods: {
            querySex() {
                this.axios
                    .request({
                        method: "get",
                        url: "./kwp/kdi/enums/Sex",
                        params: {}
                    })
                    .then(response => {
                        this.sexOptions = response.data;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            submit() {
                this.$refs['user'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.user = replaceSpacesObj(this.user);
                    this.axios.request({
                        method: "post",
                        url: "./kwp/kdi/user",
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: {
                            name: this.user.name,
                            sex: this.user.sex,
                            birthday: this.user.birthday
                        }
                    }).then(response => {
                        this.user.uuid = response.data.uuid;
                        this.info = "用户";
                        this.increasePhoneNbr();
                    }).catch(error => {
                        console.log(error);
                        this.info = '用户新增失败';
                        this.infoShow = true;
                    });
                });
            },
            increasePhoneNbr() {
                this.axios.request({
                    method: "post",
                    url: "./kwp/kdi/user/" + this.user.uuid + "/vouer",
                    headers: {
                        "Content-Type": "application/x-www-form-urlencoded"
                    },
                    data: this.qs.stringify({
                        code: this.user.codePhoneNumber,
                        vou: this.user.vouPhoneNumber
                    })
                }).then(() => {
                    this.info = this.info + "、手机号";
                    this.endProcessing();
                }).catch(error => {
                    console.log(error);
                    // if (log === "noJobNo") {
                    //     this.info = '手机号新增失败';
                    // } else {
                    //     this.info = '用户新增成功，手机号新增失败';
                    // }

                    this.infoShow = true;
                })
            },
            endProcessing() {
                this.info = this.info + '新增成功,两秒后关闭';
                this.infoShow = true;
                setTimeout(() => {
                    this.infoShow = false;
                    this.$emit("editState", false);
                    this.cleanUser();
                    this.info = '';
                }, 2000);
            },
            cleanUser() {
                this.user = {
                    name: '',
                    sex: '',
                    birthday: '',
                    vouPhoneNumber: 'Mobile',
                    codePhoneNumber: ''
                };
            },
            editOut() {
                this.infoShow = false;
                this.cleanUser();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 334px;
        height: 100%;
        z-index: 100;
    }


</style>